<?php
$manager_host = "91.233.15.136";
$manager_user = "admin";
$manager_pass = "sudobash";
$manager_port = "5038";

/* Connection timeout */
$manager_connection_timeout = 300;

/* Connect to the manager */
$fp = @fsockopen($manager_host, $manager_port, $errno, $errstr, $manager_connection_timeout);

if (!$fp) {
    
    echo "ERROR: $errstr (ERROR N: $errno)\n";
    
} else {

    $login = "Action: login\r\n";
    $login .= "Username: $manager_user\r\n";
    $login .= "Secret: $manager_pass\r\n";
    $login .= "Events: Off\r\n";
    $login .= "\r\n";
    fwrite($fp,$login);

    $manager_version = fgets($fp);

    $cmd_response = fgets($fp);

    $response = fgets($fp);

    $blank_line = fgets($fp);

    if (substr($response,0,9) == "Message: ") {
        /* We have got a response */
        $loginresponse = trim(substr($response,9));
        if (!$loginresponse == "Authentication Accepted") {
            echo "-- Unable to log in: $loginresponse\n";
            fclose($fp);
            exit(0);
        } else {
            $checkpeer = "Action: Command\r\n";
            $checkpeer .= "Command: core show hints \r\n";
            $checkpeer .= "\r\n";
            fwrite($fp,$checkpeer);
            $line = trim(fgets($fp));
            
            $found_entry = false;
            $table       = '<table id="box-table-b" style="float:left;">';
            $i=0;
            while ($line != "--END COMMAND--") { 
                if (($i-4)%16==0) {$table.="</td><table style=\"float:left;\" id=\"box-table-b\">"; } ;
                    switch (true) {
                    
                        case strpos($line,"State:Idle"):                           
                            $table .= '<tr>';
                                $table .= '<td>'.substr($line, 0, 3).'</td>';
                                $table .= '<td><img  width="30" src="media/images/free.png" title="თავისუფალი" /></td>';
                            $table .= '</tr>';
                        break;
                        
                        case strpos($line,"State:InUse"):
                            $table .= '<tr>';
                                $table .= '<td>'.substr($line, 0, 3).'</td>';
                                $table .= '<td><img  width="30" src="media/images/busy.png" title="საუბრობს" /></td>';
                            $table .= '</tr>';
                        break;
                        
                        case strpos($line,"State:Unavailable"):
                            $table .= '<tr>';
                                $table .= '<td>'.substr($line, 0, 3).'</td>';
                                $table .= '<td><img width="30" src="media/images/unavailable.png" title="მიუწვდომელი" /></td>';
                            $table .= '</tr>';
                        break;
                        
                        case strpos($line,"State:Unknow"):
                            $table .= '<tr>';
                                $table .= '<td>'.substr($line, 0, 3).'</td>';
                                $table .= '<td><font size="2" color="red">უცნობი</font></td>';
                            $table .= '</tr>';
                        break;
                    }
                
                $i++;
                               
                $line = trim(fgets($fp));
            }
            
            $table .= '</table>';
            echo $table;
            fclose($fp);
            exit(0);
        }
    } else {
        echo "Unexpected response: $response\n";
        fclose($fp);
        exit(0);
    }
}
?>